
//
//  NSString+Regex.swift
//  正则表达式
//
//  Created by zhangguo on 15/10/26.
//  Copyright © 2015年 zhangguo. All rights reserved.
//

import Foundation

extension String {
    
    func hrefLink()->(link:String?,text:String?) {
        
    //元组，一次可以返回多个值
    //1找特征字符
    let pattern = "<a href=\"(.*?)\".*?>(.*?)</a>"
    
    let regex = try! NSRegularExpression(pattern: pattern, options: NSRegularExpressionOptions.DotMatchesLineSeparators)
    
        //开始匹配
        if let result = regex.firstMatchInString(self, options: NSMatchingOptions(rawValue: 0), range: NSMakeRange(0, self.characters.count)) {
            
            let r = result.rangeAtIndex(1)
            let r2 = result.rangeAtIndex(2)
            
            let link = (self as NSString).substringWithRange(r)
            let text = (self as NSString).substringWithRange(r2)
            
            return (link,text)

        }
            return (nil,nil)
    }

}